package com.mazaiting.instructions.stack;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;
import com.mazaiting.rtda.MtOperandStack;
import com.mazaiting.rtda.MtSlot;

/**
 * 交换栈顶的两个元素
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class SwapInstruction {

    /**
     * 交换栈顶的两个元素
     * bottom -> top
     * [...][c][b][a]
     *           \/
     *           /\
     *          V  V
     * [...][c][a][b]
     */
    public static class Swap extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            MtOperandStack stack = frame.getOperandStack();
            MtSlot slot1 = stack.popSlot();
            MtSlot slot2 = stack.popSlot();
            stack.pushSlot(slot2);
            stack.pushSlot(slot1);
        }
    }

}
